package com.suliao.common.base;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author： M1nG
 * @since： 2020/9/15 18:41
 * @description： 通用的接受前端分页参数的类
 * @version： 1.0
 */
public class PageFactory {

    public static Page createPageInfo() {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Integer current = Integer.valueOf(Optional.ofNullable(request.getParameter("current")).orElse("1"));
        Integer size = Integer.valueOf(Optional.ofNullable(request.getParameter("size")).orElse("10"));
        return new Page(current.longValue(), size.longValue());
    }

    public static Page createPageInfo(Integer current, Integer size) {
        Integer OptCurrent = Integer.valueOf(Optional.ofNullable(current).orElse(1));
        Integer OptSize = Integer.valueOf(Optional.ofNullable(size).orElse(10));
        return new Page(OptCurrent.longValue(), OptSize.longValue());
    }

    public static Result<Page> defaultResult() {
        Page pageInfo = createPageInfo();
        return Result.success(pageInfo);
    }

}
